package com.coconet.gas.rest.sys;


import com.coconet.gas.abs.AbstractBaseController;
import com.coconet.gas.service.SysUserService;
import com.coconet.gas.vo.dw.SysUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;
import response.ObjectRestResponse;


@CrossOrigin
@RestController
@RequestMapping(value = "/user")
public class LoginRest extends AbstractBaseController {
	@Autowired
	private SysUserService userService;

	/**
	 * 用户登录
	 *
	 * @param username
	 *            用户名
	 * @param password
	 *            密码
	 * @return 操作结果
	 * @throws AuthenticationException
	 *             错误信息
	 */

	@ApiOperation(value = "登录", httpMethod = "POST")
	@PostMapping(value = "/login")
	public ObjectRestResponse getToken(@RequestBody SysUser user) throws AuthenticationException {
		getRequest().setAttribute("username", user.getUsername());
		String jwtToken= userService.login(user.getUsername(), user.getPassword());
		//LogManager.me().executeLog(LogTaskFactory.loginLog(user.getUsername(), getIp()));
		return new ObjectRestResponse<String>(200, "登录成功",jwtToken);
	}

	/**
	 * 查询菜单
	 *
	 * 
	 * @return 操作结果
	 */
	@ApiOperation(value = "根据身份查询菜单", httpMethod = "GET")
	@GetMapping(value = "/getMenu")
	public ObjectRestResponse getMenu() throws AuthenticationException {

		String authorities = getAuthorities();
		if (authorities.contains("vadmin")) {

			return new ObjectRestResponse<String>(200, "登录成功", "sss ");
		}

		return new ObjectRestResponse<String>(200, "登录成功", "sss ");
	}

}
